<html>
<head>
  <title>Sepkoski Curve</title>
	<SCRIPT LANGUAGE="JavaScript">
function ajaxname(){ //called in "Free Form search" part when clicking "search" button

  //document.getElementById('foo_freesearch').innerHTML = '<img src=\"loading.gif\"> Please Wait, loading data...</div>'; //April 17

  var ajaxRequest;  // The variable that makes Ajax possible!
    try{
      // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
      // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    try{
        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e){
      // Something went wrong
        alert("Your browser broke!");
        return false;
    }
    }
    }
  // Create a function that will receive data sent from the server
  ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){
      //document.getElementById('foo_freesearch').innerHTML = ""; //April 17
      var ajaxDisplay = document.getElementById('ajaxnameDiv');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
  }
  
  var newnametxt = document.getElementById('textname').value;
	//var searchtxt ="It is just a test!";
  var queryString = "?newnametxt=" + newnametxt;
	ajaxRequest.open("GET", "newname.php" + queryString, true);
	ajaxRequest.send(null);
}
  
  </script>
 

<?php
    include 'header.php';
  ?>
<body>

<?php
$username = "fossilplot";
$password = "fossilplot";
$hostname = "localhost"; 
//echo "hello";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
 or die("Unable to connect to MySQL");


//select a database to work with
$selected = mysql_select_db("fossilplot",$dbhandle)  or die("Could not select examples");
$cm_classname= "("."'Cl. Polychaeta'".","."'Cl. Orthothecimorpha'".","."'Cl. Hyolithomorpha'".","."'Cl. Helcionelloida \"Monoplacophora\"'".","."'Cl. Helcionelloida \"Monoplacophora\"'".","."'Cl. Trilobita'".",". "'Cl. Inarticulata'".",". "'Cl. Eocrinoidea'".")";
$md_classname="("."'Cl. Demospongia'".","."'Cl. Gastropoda'".","."'Cl. Bivalvia'".","."'Sc. Crustacea, Cl. Malacostraca'".","."'Cl. Gymnolaemata'".","."'Cl. Echinoidea'".","."'Cl. Chondrichthyes'".","."'Cl. Osteichthyes'".")";
$pz_classname="("."'Cl. Anthozoa'".","."'Cl. Cephalopoda'".","."'Sc. Crustacea, Cl. Ostracoda'".","."'Cl. Stenolaemata'".","."'Cl. Articulata'".","."'Asterozoa, Cl. Asteroidea'".","."'Cl. Crinoidea'".","."'Cl. Blastoidea'".")";

  
  
  //$genusId= "("."1".","."2".")";
  //echo "SELECT * FROM genus where class_name in $pz_classname";
  $resulttt = mysql_query("SELECT fad_date, lad_date FROM genus where class_name in $cm_classname");
  $resultmd = mysql_query("SELECT fad_date, lad_date FROM genus where class_name in $md_classname");
  $resultpz = mysql_query("SELECT fad_date, lad_date FROM genus where class_name in $pz_classname");


  
$epoch_arr=array();//for cm
$epoch_arr2=array();//for md classname
$epoch_arr3=array();//for pz classname
$epoch_r=mysql_query("select * from stgintv where id>0");
$i=1;
while($row=mysql_fetch_array($epoch_r)){
	$epoch_arr[$i]=0;
	$i++;
}


while ($rowtt = mysql_fetch_array($resulttt)) {
	$epoch_r=mysql_query("select * from stgintv where id>0");

	while($row=mysql_fetch_array($epoch_r)){
		if(($rowtt{'fad_date'} < $row{'begin'} && $rowtt{'fad_date'} >= $row{'end'})||($rowtt{'lad_date'} < $row{'begin'} && $rowtt{'lad_date'} >= $row{'end'}))
		{
			
			$epoch_arr[$row{'id'}]++;
		}
	}	
	  
   //echo "name:".$rowtt{'class_name'}." owner:".$rowtt{'fad_date'}."<br>";
}
//for($j=1;$j<=sizeof($epoch_arr); $j++)
	//echo "<br>arr $j is". $epoch_arr[$j];


$epoch_md=mysql_query("select * from stgintv where id>0");
$i=1;
while($rowmd=mysql_fetch_array($epoch_md)){
	$epoch_arr2[$i]=0;
	$i++;
}


while ($rowttmd = mysql_fetch_array($resultmd)) {
	$epoch_md=mysql_query("select * from stgintv where id>0");

	while($rowmd=mysql_fetch_array($epoch_md)){
		if(($rowttmd{'fad_date'} < $rowmd{'begin'} && $rowttmd{'fad_date'} >= $rowmd{'end'})||($rowttmd{'lad_date'} < $rowmd{'begin'} && $rowttmd{'lad_date'} >= $rowmd{'end'}))
		{
			
			$epoch_arr2[$rowmd{'id'}]++;
		}
	}	  
}


//for($j=1;$j<=sizeof($epoch_arr2); $j++)
	//echo "<br>arr2 $j is". $epoch_arr2[$j];	
	
	
	
	
	
$epoch_pz=mysql_query("select * from stgintv where id>0");
$i=1;
while($rowpz=mysql_fetch_array($epoch_pz)){
	$epoch_arr3[$i]=0;
	$i++;
}


while ($rowttpz = mysql_fetch_array($resultpz)) {
	$epoch_pz=mysql_query("select * from stgintv where id>0");

	while($rowpz=mysql_fetch_array($epoch_pz)){
		if(($rowttpz{'fad_date'} < $rowpz{'begin'} && $rowttpz{'fad_date'} >= $rowpz{'end'})||($rowttpz{'lad_date'} < $rowpz{'begin'} && $rowttpz{'lad_date'} >= $rowpz{'end'}))
		{
			
			$epoch_arr3[$rowpz{'id'}]++;
		}
	}	  
}


//for($j=1;$j<=sizeof($epoch_arr3); $j++)
	//echo "<br>arr3 $j is". $epoch_arr3[$j];	
	
	
$File = "./specialgrh1.php"; 
$Handle = fopen($File, 'w');
$Data = "<?php	
	
include (\"./src/jpgraph.php\");
include (\"./src/jpgraph_line.php\");
\$datax=array";
fwrite($Handle, $Data);
$epoch_r=mysql_query("select * from stgintv where id=1");

$epoch="(";
while($row=mysql_fetch_array($epoch_r)){
		
	$epoch=$epoch."\"".$row{'stage_interval'}."\"";
}
		
$epoch_r=mysql_query("select * from stgintv where id>1") or die ("error selecting from stgintv!!");

while($row=mysql_fetch_array($epoch_r)){
		
	$epoch=$epoch.","."\"".$row{'stage_interval'}."\"";
}
$epoch=$epoch.")";
$Data="$epoch;
\$datay=array";
fwrite($Handle, $Data);
$bary="(\"$epoch_arr[1]\"";
for($i=2; $i<=sizeof($epoch_arr); $i++){
  $bary =$bary.","."\"".$epoch_arr[$i]."\"";
}
$bary=$bary.")";
$Data="$bary;

\$datay2=array";
fwrite($Handle, $Data);
$bary2="(\"$epoch_arr2[1]\"";
for($i=2; $i<=sizeof($epoch_arr2); $i++){
  $bary2 =$bary2.","."\"".$epoch_arr2[$i]."\"";
}
$bary2=$bary2.")";
$Data="$bary2;

\$datay3=array";
fwrite($Handle, $Data);
$bary3="(\"$epoch_arr3[1]\"";
for($i=2; $i<=sizeof($epoch_arr3); $i++){
  $bary3 =$bary3.","."\"".$epoch_arr3[$i]."\"";
}
$bary3=$bary3.")";
$Data="$bary3;

\$graph = new Graph(800,450,\"auto\");
\$graph->SetScale(\"textlin\");

\$graph->img->SetMargin(50,30,20,80);    
\$graph->SetShadow();

\$graph->title->Set(\"Sepkoski Curve/Special Graph 1\");
\$graph->title->SetFont(FF_FONT1,FS_BOLD);


\$dplot = new LinePLot(\$datay);
\$dplot2 = new LinePLot(\$datay2);
\$dplot3 = new LinePLot(\$datay3);

\$dplot->SetFillColor(\"blue\");
\$dplot2->SetFillColor(\"green\");
\$dplot3->SetFillColor(\"red\");

\$dplot->SetLegend(\"Cm\");
\$dplot2->SetLegend(\"Md\");
\$dplot3->SetLegend(\"Pz\");


// Create the accumulated graph
\$accplot = new AccLinePlot(array(\$dplot,\$dplot3, \$dplot2));

// Add the plot to the graph
\$graph->Add(\$accplot);

//\$p1 = new LinePlot(\$datay);
//\$p1->SetFillColor(\"blue\");
//\$p1->mark->SetType(MARK_FILLEDCIRCLE);
//\$p1->mark->SetFillColor(\"red\");
//\$p1->mark->SetWidth(4);
\$graph->yaxis->title->SetMargin(10);

\$graph->yaxis->title->Set(\"Number of Genera\");
\$graph->xaxis->SetTickLabels(\$datax);
\$graph->xaxis->SetLabelAngle(90);
\$graph->xaxis->title->SetMargin(40);
\$graph->xaxis->title->Set(\"Epoch Interval\");
\$graph->Stroke();
?>";

fwrite($Handle, $Data);


$myFile = "./jpgraph_cache/specialgrh1.png";
    if(file_exists($myFile)){
    unlink($myFile);
   }
echo "Provide a New Name for the Graph: ";
echo "<input name=\"textname\" type=\"text\" id=\"textname\" size=\"35\" />
<input type=\"button\" name=\"givename\" id=\"givename\" onclick='ajaxname()' value=\"Change Name\" /><br>
	<br>
		
	</br>";
echo "<div id='ajaxnameDiv'>";
//echo "<img src=\"http://localhost/test/graph/specialgrh1.php\">";
echo "<img src=\"http://www.fossilplot.org/test/graph/specialgrh1.php\">";
//echo "<br><br><a href=\"http://localhost/test/graph/jpgraph_cache/specialgrh1.png\" target=\"_blank\"><font size=\"1\"> Download/Print Graph</font></a>";
echo "<br><br><a href=\"http://www.fossilplot.org/test/graph/jpgraph_cache/specialgrh1.png\" target=\"_blank\"><font size=\"1\"> Download/Print Graph</font></a>";

echo "</div>";
?>

<?php include 'footer.php';?>

</body>
</html>